<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Mycommon
{
    
    private $_CI;
    
    public function __construct()
    {
        $this->_CI =& get_instance();
    }

    public function getLangCode()
    {
        if ($this->_CI->session->userdata('lang')) {
            $lang = $this->_CI->session->userdata('lang');
            $lang = ($lang == '') ? $CI->config->item('language') : $lang;
        } else {
            $lang = $this->_CI->config->item('language');
            $this->_CI->session->set_userdata('lang', $lang);
        }
        return $lang;
    }
    
    public function setLangCode($langCode)
    {
        $this->_CI->session->set_userdata('lang', $langCode);
        return true;
    }
    
    public function fetchLangCode($lang)
    {
        $langCode = '';
        switch ($lang)
        {
            case 'vietnam'  : $langCode = 'vn';break;
            case 'english'  : $langCode = 'en';break;
            case 'japan'    : $langCode = 'jp';break;
        }
        return $langCode;
    }
    
    public function decodeLang($lang)
    {
        $langCode = '';
        switch ($lang) {
            case 'vn' : $langCode = 'vietnam';break;
            case 'en' : $langCode = 'english';break;
            case 'jp' : $langCode = 'japan';break;
        }
        return $langCode;
    }
    
    public function authAdmin()
    {
        $uri = $this->_CI->uri->uri_string();
        if(!$this->_CI->session->userdata('u_id') || !$this->_CI->session->userdata('u_acc')) {
            $backLink = urlencode(site_url($uri));
            redirect(site_url('admin/login') . '?back=' . $backLink);
        }
    }

}

?>
